#include <QtGui/QApplication>
#include "mainwindow.h"
#include "globals.h"
#include "datasource.h"



void myMessageOutput(QtMsgType type, const char *msg)
{
    static bool semaphore=false;
    bool file_out = !semaphore;
    semaphore = true;
    QFile f("debug.log");
    if (file_out)
        f.open(QIODevice::Append | QIODevice::Text);
    QString s;
    bool fatal=false;
    switch (type) {
    case QtDebugMsg:
        s = QString("Debug: %1 \r\n").arg(msg);
        break;
    case QtWarningMsg:
        s = QString("Warning: %1 \r\n").arg(msg);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical: %s\n", msg);
        s = QString("Critical: %1 \r\n").arg(msg);
        break;
    case QtFatalMsg:
        fatal =true;
        s = QString("Fatal: %1 \r\n").arg(msg);
        break;
    }
    QDateTime cur = QDateTime::currentDateTime();
    QString date = cur.toString("[d.MM.yy / h:mm:ss] ");
    s = date + s;
    f.write(s.toAscii());
    fputs(s.toAscii(),stderr);
    fflush(stderr);
    f.close();
    semaphore = false;
    if (fatal) abort();
}





int main(int argc, char *argv[])
{
    qInstallMsgHandler(myMessageOutput);
    qDebug() << "Program: starts";
    QApplication a(argc, argv);
    globals = new CGlobals;
    dataSourceFactory = new FDataSourceFactory;

    MainWindow *w = new MainWindow();

    w->show();
    int res =  a.exec();
    delete w;
    delete dataSourceFactory;
    delete globals;
    qDebug() << "Program: Ends";
    return res;
}
